package com.java.mybatis.controller;

import com.java.mybatis.entity.Goods;
import com.java.mybatis.entity.Ordergoods;
import com.java.mybatis.entity.Putinlocation;
import com.java.mybatis.service.GoodsService;
import com.java.mybatis.service.OrdergoodsService;
import com.java.mybatis.service.PutinlocationService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (Putinlocation)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:27
 */
@Controller
@RequestMapping("/putinlocation/")
public class PutinlocationController {
    /**
     * 服务对象
     */
    @Resource
    private PutinlocationService putinlocationService;

    @Resource
    private OrdergoodsService ordergoodsService;

    @Resource
     private GoodsService goodsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Putinlocation selectOne(Integer id) {
        return this.putinlocationService.queryById(id);
    }

    @RequestMapping("SelAllPutIn")
    public String SelAllPutIn(Model model, Integer page, HttpSession session){
        try {
            session.removeAttribute("NameLike");
            //分页查询
            List<Putinlocation> allPutIn = putinlocationService.getAllPutIn((page-1)*7,7);
            for (Putinlocation putinlocation : allPutIn) {
                System.out.println(putinlocation);
            }
            model.addAttribute("allPutIn",allPutIn);
            //查询总行数
            Integer count = putinlocationService.SelPutCount();
            model.addAttribute("count",count);
            //当前页码
            session.setAttribute("thisPutInPage",page);
            int goodPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
            model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
            //存入总共有多少页
            model.addAttribute("GoodsPageMax",goodPageMax);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "System/member-list";
    }

    @RequestMapping("upPuInCode")
    public String upPuInCode(Integer id,HttpSession session){
        try {
            putinlocationService.UpdateCode(new SimpleDateFormat("yyyy-MM-dd").format(new Date()),id);
            Ordergoods ordergoods = ordergoodsService.selSomeOne(id);
           goodsService.insertGoods
                   (ordergoods.getOrdergoodsOrder(),ordergoods.getOrdergoodsName(),ordergoods.getOrdergoodsNumber().toString(),ordergoods.getUserName(),ordergoods.getOrdergoodsWeight(),ordergoods.getUserPhone(),ordergoods.getUserAddress(),ordergoods.getOrder().getOrderId(),ordergoods.getGoodstype().getGoodstypeId(),ordergoods.getLocation().getLocationId(),1,1,ordergoods.getLocation().getLocationId());
           putinlocationService.updateLocationState(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer thisPutInPage = (Integer) session.getAttribute("thisPutInPage");
        return "redirect:/putinlocation/SelAllPutIn?page="+thisPutInPage;
    }

    @RequestMapping("upAllPutInCode")
    public String upAllPutInCode(String str,HttpSession session){
        try {
            String[] split = str.split(",");
            for (String s : split) {
                putinlocationService.UpdateCode(new SimpleDateFormat("yyyy-MM-dd").format(new Date()),Integer.parseInt(s));
                Ordergoods ordergoods = ordergoodsService.selSomeOne(Integer.parseInt(s));
                goodsService.insertGoods
                        (ordergoods.getOrdergoodsOrder(),ordergoods.getOrdergoodsName(),ordergoods.getOrdergoodsNumber().toString(),ordergoods.getUserName(),ordergoods.getOrdergoodsWeight(),ordergoods.getUserPhone(),ordergoods.getUserAddress(),ordergoods.getOrder().getOrderId(),ordergoods.getGoodstype().getGoodstypeId(),ordergoods.getLocation().getLocationId(),1,1,ordergoods.getLocation().getLocationId());
                putinlocationService.updateLocationState(Integer.parseInt(s));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer thisPutInPage = (Integer) session.getAttribute("thisPutInPage");
        return "redirect:/putinlocation/SelAllPutIn?page="+thisPutInPage;
    }

    @RequestMapping("selNameLike")
    public String selNameLike(Model model, String name,Integer page,HttpSession session){
        try {
          if(name!=null&&name!=""){
              List<Putinlocation> putinlocations = putinlocationService.selNameLike(name, (page - 1) * 5, 5);
              model.addAttribute("allPutIn",putinlocations);
              Integer count = putinlocationService.selLikeCount(name);
              model.addAttribute("count",count);
              session.setAttribute("thisPutInPage",page);
              int goodPageMax = count % 5 == 0 ? count / 5 : count / 5 + 1;
              model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
              //存入总共有多少页
              model.addAttribute("GoodsPageMax",goodPageMax);
              session.setAttribute("NameLike",name);
          }else {
              return "redirect:/putinlocation/SelAllPutIn?page=1";
          }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/member-list";
    }

    @RequestMapping("upPutCode")
    public String upPutCode(Integer id,HttpSession session){
        try {
            putinlocationService.upPutCode(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer thisPutInPage = (Integer) session.getAttribute("thisPutInPage");
        return "redirect:/putinlocation/SelAllPutIn?page="+thisPutInPage;
    }
}